package com.oss.robot.project;

import org.htmlparser.Parser;
import org.htmlparser.filters.AndFilter;
import org.htmlparser.filters.HasAttributeFilter;
import org.htmlparser.filters.HasParentFilter;
import org.htmlparser.filters.LinkStringFilter;
import org.htmlparser.filters.TagNameFilter;
import org.htmlparser.tags.LinkTag;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.ParserException;

import com.oss.model.Project;
import com.oss.robot.BaseRobot;

/**
 * @author xueqiang.mi
 * @since 2010-7-28
 */
public class SpringSourceRobot extends BaseRobot {

	static public final String URL = "http://www.springsource.org/";
	static public final String SOURCE = "SpringSource";
	static public final String LICENSE = "Apache License 2.0";

	public void retrieveProjects() throws ParserException {
		Parser parser = new Parser(URL);
		NodeList nodeList = parser
				.extractAllNodesThatMatch(new HasAttributeFilter("id",
						"menu-4409"));
		nodeList = nodeList.extractAllNodesThatMatch(new AndFilter(
				new LinkStringFilter("/"), new HasParentFilter(
						new TagNameFilter("li"))), true);

		for (int i = 1; i < nodeList.size(); i++) {
			LinkTag linkTag = (LinkTag) nodeList.elementAt(i);
			String name = linkTag.getFirstChild().getText();
			String url = linkTag.getLink();
			
			Project prj = new Project(name, LICENSE, SOURCE, url);
			prj.setLabel(name);
			prj.setTag("others");
			prj.setEnabled(true);
			prjService.add(prj);
		}
	}
}
